package cn.sanch.org.controller;

import cn.sanch.basic.annotation.PreAuthorize;
import cn.sanch.basic.util.AjaxResult;
import cn.sanch.basic.util.PageList;
import cn.sanch.org.domain.Department;
import cn.sanch.org.query.DepartmentQuery;
import cn.sanch.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 * @author sanch37
 * 2022/5/26
 */
@RestController
@RequestMapping("/department")
@Api(value = "宠物之家的部门相关API", description = "部门的CRUD操作")
public class DepartmentController {


    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口：查询部门当个对象
     * @param id 通过ID查询
     * @return 返回部门单个对象
     *
     * GetMapping 等价于：@RequestMapping(method = {RequestMethod.GET})
     * PathVariable Long id 将地址栏的参数给id
     */
    @ApiOperation(value = "通过ID查询部门单个对象")
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个部门", sn = "department:getOne")
    public Department queryOne(@PathVariable Long id) {

        return departmentService.findOne(id);

    }

    /**
     * 接口：查询部门所有对象
     * @return 返回部门所有对象
     *
     * GetMapping 等价于：@RequestMapping(method = {RequestMethod.GET})
     */
    @ApiOperation(value = "查询部门所有对象")
    @GetMapping
    @PreAuthorize(name = "查询所有部门", sn = "department:getAll")
    public List<Department> queryAll() {
        return departmentService.findAll();
    }

    /**
     * 接口：分页查询；高级查询
     * @param departmentQuery 通过分页信息查询
     * @return 返回分页所对应的对象
     *
     * PostMapping 等价于：@RequestMapping(method = {RequestMethod.POST})
     * RequestBody 从请求体【请求数据包中】中获取数据 赋值给DepartmentQuery对象
     */
    @ApiOperation(value = "分页查询；高级查询")
    @PostMapping
    @PreAuthorize(name = "分页和高级查询", sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 接口：删除单个部门
     * @param id 通过ID查询
     * @return 返回增删改时响应给前端的数据
     */
    @ApiOperation(value = "通过ID删除部门单个对象")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门", sn = "department:delete")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口：添加或者修改
     * @param department 通过部门对象
     * @return 返回增删改时响应给前端的数据
     *
     *
     * RequestBody 从请求体【请求数据包中】中获取数据 赋值给Department对象
     */
    @ApiOperation(value = "添加或者修改")
    @PutMapping
    @PreAuthorize(name = "添加修改部门", sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }

    }


    /**
     * 接口：批量删除部门
     * @param ids ids
     * @return AjaxResult
     */
    @ApiOperation(value = "批量删除部门对象")
    @PatchMapping
    @PreAuthorize(name = "批量删除部门", sn = "department:patchDelete")
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }


    /**
     * 接口：部门树
     * @return List<Department>
     */
    @ApiOperation(value = "获取部门树")
    @GetMapping("/deptTree")
    @PreAuthorize(name = "部门树", sn = "department:deptTree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}
